<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- <span>{{ rowData }}</span> -->
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            :inline="false"
            label-width="120px"
        >
            <el-form-item label="仓库ID" prop="id">
                <el-input v-model="id" placeholder="" clearable></el-input>
            </el-form-item>
            <el-form-item label="仓库url" prop="url">
                <el-input v-model="url" placeholder="" clearable></el-input>
            </el-form-item>
            <el-form-item label="用户名" prop="username">
                <el-input
                    v-model="username"
                    placeholder=""
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input
                    v-model="password"
                    placeholder=""
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="是否默认" prop="isDefault">
                <el-radio-group v-model="isDefault">
                    <el-radio-button :label="true">是</el-radio-button>
                    <el-radio-button :label="false">否</el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>

        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, toRefs, defineProps, defineEmits } from "vue";

const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const closeDialog = () => {
    emit("closeDialog", false);
};
const form = reactive({
    id: "",
    url: "",
    username: "",
    password: "",
    isDefault: false,
});
const { id, url, username, password, isDefault } = toRefs(form);
const formRef = ref(null);
const rules = {
    id: [
        {
            required: true,
            message: "请输入仓库id",
            trigger: "blur",
        },
    ],
    url: [
        {
            required: true,
            message: "请输入仓库url",
            trigger: "blur",
        },
    ],
    username: [
        {
            required: true,
            message: "请输入账号",
            trigger: "blur",
        },
    ],
    password: [
        {
            required: true,
            message: "请输入密码",
            trigger: "blur",
        },
    ],
    isDefault: [
        {
            required: true,
            message: "请选择是否是默认",
            trigger: "blur",
        },
    ],
};

/**
 * @description: 数据初始化
 * @param {*}
 * @return {*}
 */
rowData.value &&
    ((id.value = rowData.value.id),
    (url.value = rowData.value.url),
    (username.value = rowData.value.username),
    (password.value = rowData.value.password),
    (isDefault.value = rowData.value.isDefault));
/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let res;
            if (title.value === "添加") {
                res = await VE_API.builder.dockerRepositoryStory(form);
            } else {
                res = await VE_API.builder.dockerRepositoryStory({
                    id: rowData.value.id,
                    ...form,
                });
            }
            const { code } = res;
            if (code === 0) {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};
</script>

<style lang="scss">
.fl {
    float: left;
}
.p0 {
    padding: 0 !important;
}
.role_edit_item {
    width: 100%;
}
</style>
